﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SubSonic;
using LinFen;
using System.Data.SqlClient;
public partial class webUserControl_myPoints : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Cookies c=new Cookies();
        if(c.getCookies("cCustomerID")=="0")
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            if(!IsPostBack)
            {
                GridView2.DataSourceID = "sqlDataSource2";
                GridView2.DataBind();
            }
        }
        else
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
        CustomerDataBind();
       //GridView2.DataBind();
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "shifang")
        {
            //Response.Write("OK");
            int oID = int.Parse(e.CommandArgument.ToString());
            //Response.Write(oID.ToString());
            StoredProcedure sp = new StoredProcedure("sp_ExcuteOrderSingle");
            sp.Command.AddParameter("@oID",oID);
            sp.Execute();
            Query q = new Query("orderinfo");
            q.AddWhere("ID", oID);
            IDataReader dr = q.ExecuteReader();
            if(dr.Read())
            {
                int NumMale = int.Parse(dr[3].ToString());
                int NumFeMale = int.Parse(dr[4].ToString());
                int NumHongGan = int.Parse(dr[5].ToString());
                int NumBaoJian = int.Parse(dr[6].ToString());
                int NumQiTa = int.Parse(dr[7].ToString());
                SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LinFen"].ConnectionString);
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                cmd.CommandText = "update customerinfo set isOKPoint = isOKPoint -" +
                                  (NumQiTa + NumMale + NumHongGan + NumFeMale + NumBaoJian) + " where id=" + dr[1];
                conn.Open();
                cmd.ExecuteNonQuery();
                OrderInfo.Delete("ID", oID);
                conn.Close();
            }
            GridView2.DataBind();
            //Response.Redirect(Request.Url.ToString());
        }
    }
    void CustomerDataBind()
    {
        Cookies c = new Cookies();
        int id = int.Parse(c.getCookies("cCustomerID"));
        SqlQuery q = new SqlQuery();
        q.From("vw_CustomerOrderSum");
        q.Where("customerid").IsEqualTo(id);
        IDataReader dr = q.ExecuteReader();
        if (dr.Read())
        {
            lbCustomerName.Text = dr["customername"].ToString();
            lbTelPhone.Text = dr["Telphone"].ToString();
            lbLinkMan.Text = dr["LinkMan"].ToString();
            lbYewu.Text = dr["YeWu"].ToString();
            lbAllPoints.Text = dr["istotalpoint"].ToString();
            lbSumPoints.Text = dr["isokPoint"].ToString();
            lbEndDate.Text = Convert.ToDateTime(dr["OutTime"].ToString()).ToShortDateString();
            lbFeMaleS.Text = dr["FemaleC"].ToString();
            lbManS.Text = dr["Malec"].ToString();
            lbHongGanS.Text = dr["hongganc"].ToString();
            lbBaoJianS.Text = dr["baojian"].ToString();
            lbQiTa.Text = dr["qitac"].ToString();

            lbSupplierC.Text = SupplierNum(id);
            if (dr["istotalpoint"].ToString() == dr["isokPoint"].ToString())
            {
                //Response.Redirect("~/PointsListLoad.aspx?ID=" + id);
            }

        }

    }
    string SupplierNum(int id)
    {
        Query q = new Query("vw_orderdetail");
        q.AddWhere("CustomerID", id);
        //q.AddWhere("status", 0);
        q.SetSelectList("suppliername");
        q.DISTINCT();
        return q.GetRecordCount().ToString();
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        UltraWebGridExcelExporter1.Export(UltraWebGrid1);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        
        if(tbCode.Text !="")
        {
            GridView2.DataSourceID = "SqlDataSource3";
            GridView2.DataBind();
        }
        else
        {
             if(tbName.Text !="")
            {
                GridView2.DataSourceID = "SqlDataSource4";
                GridView2.DataBind();
            }
             else
             {
                 GridView2.DataSourceID = "SqlDataSource2";
                 GridView2.DataBind();
             }
        }
       
    }
}
